﻿/** 
 * @auther wwj
 * @version 1.0.1
 * Create On 2012-12-10
 */

// 扩展jQuery
jQuery.myValidData = {
	checkLength:function(obj,objMin,objMax){
		if(obj.length >= objMin && obj.length <= objMax){
			return true;
		}else{
			return false;
		}
	},
	checkNumeric:function(obj){
		if (true == /^\d+$/.test(obj)){
		   return true;
		}else{
		   return false;
		}
	},
	checkUrl:function(obj){
		var myReg = /^http:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=#]*)?$/; 
		if(true == myReg.test(obj)){
			return true; 
		}else{
			return false;
		}
	}
};

// 方法扩展
jQuery.fn.extend({
	// 选中相应的select列表
	doSelected: function(obj) {  
		return this.each(function() {
			if (this.value == obj)
			{
				this.selected = true;
			}
		});
	},
	// 全选
	doCheckAll: function(){
		var selectItem = $(this).val();
		$(":checkbox[name='"+selectItem+"']").attr("checked", $(this).attr("checked"));
	},
	// 获取check元素中选中的值
	getChecked:function(){
		var listID = new Array();
		this.each(function(){
			listID.push(this.value);
		});
		return listID;
	},
	deleteAction: function() {
		return this.each(function() { this.checked = false; });
	},
	// 列出地区select表单
	districtList:function(url, upid, selected){
		var obj = this;
		var option = "<option value=''>请选择</option>";
		var subid = '';
		$.ajax({
			type:"POST",
			dataType:"json",
			async: false,
			url:url,
			data:{"upid":upid,"selected":selected},
			error:function(){
				obj.empty();
				obj.append(option);
			},
			success:function(json) {
				if (json['option'] != '') {
					obj.empty();
					obj.append(option);
					obj.append(json['option']);
					// 选中项的id，以便列出下一级列表，目前并未使用
					subid = json['subid'];
				}
			}
		});
		return subid;
	},
	// 根据省地区的选择列出市地区的列表
	districtChange:function(url, selected){
		var obj = this;
		var selectedID = $('#province option:selected').attr('id');	
		if(selectedID == '' || 'undefined' == selectedID){
			obj.empty();
			obj.append("<option value=''>请选择</option>");
		}else{
			var upid = selectedID.substr(3);
			obj.districtList(url,upid,selected);
		}
	}
});